package com.rdc.example.controller;

import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.ArticleDTO;
import com.rdc.example.bean.dto.ColumnDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.service.impl.ArticleServiceImpl;
import com.rdc.example.service.impl.ColumnServiceImpl;
import com.rdc.example.view.ColumnView;
import com.rdc.example.view.UtilView;

import java.util.List;

import static com.rdc.example.controller.UserController.utilView;

/**
 * @author duck
 */
public class ColumnController {
    private static final ColumnServiceImpl columnService=new ColumnServiceImpl();
    private static final ArticleServiceImpl articleService=new ArticleServiceImpl();
    private static final ColumnView columnView=new ColumnView();

    public static void insertColumn(Integer userId) {
        if (userId==null){
            utilView.showError("Id is null");
            return;
        }
        try{
            ColumnDTO columnDTO=new ColumnDTO();
            ColumnView.insertColumn(columnDTO);
            if (columnDTO.getName()==null){
                utilView.showError("专栏名称不为空");
                return;
            }
            columnDTO.setUserId(userId);
            Result result =columnService.insertColumn(columnDTO);
            System.out.println(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void deleteColumn(List<ColumnDTO> columnDTOList, UserDTO userDTO){
        if (columnDTOList.isEmpty()){
            utilView.showMessage("没有专栏可操作，空空如也");
            return;
        }
        Integer index = utilView.getUserInputInteger("请输入专栏编号");
        if (index > columnDTOList.size()  || index < 1) {
            utilView.showMessage("没有对应的专栏");
            return;
        }
        ColumnDTO columnDTO=columnDTOList.get(index-1);
        //为管理员（isTop=1）或者是自己的专栏（article.userId=user.id）,
        if(!columnDTO.getUserId().equals(userDTO.getId())&&!userDTO.getPower()){
            utilView.showMessage("权限不足！");
            return;
        }
        try{
            Integer columnId = columnDTOList.get(index - 1).getId();
            Result result = columnService.deleteColumn(columnId);
            utilView.showMessage(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static void updateColumn(List<ColumnDTO> columnDTOList) {
        if (columnDTOList.isEmpty()){
            utilView.showMessage("没有专栏可操作，空空如也");
            return;
        }
        Integer index = utilView.getUserInputInteger("请输入专栏编号：");
        if (index > columnDTOList.size()  || index < 1) {
            utilView.showMessage("没有对应的专栏");
            return;
        }
        try{
            ColumnDTO columnDTO = columnDTOList.get(index - 1);
            columnDTO.setName(UtilView.getUserInputString("专栏标题："));
            Result result = columnService.updateColumn(columnDTO);
            utilView.showMessage(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void showListColumnByUserId(Integer userId){
        if (userId == null){
            utilView.showError("Id is null");
            return;
        }
        try{
            List<ColumnDTO> columnDTOList = columnService.listColumnByUserId(userId);
            showColumnList(columnDTOList);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void showColumnList(List<ColumnDTO> columnDTOList){
        if(columnDTOList.isEmpty()){
            utilView.showMessage("没有栏目，空空如也");
        }
        for(int i = 0; i < columnDTOList.size(); i++){
            ColumnDTO dto = columnDTOList.get(i);
            ColumnView.showColumn(i, dto);
        }
        MainController.currentColumnDTOList = columnDTOList;
    }

    public static boolean turnToColumn(List<ColumnDTO> columnDTOList){
        if (columnDTOList.isEmpty()){
            utilView.showMessage("没有栏目可操作，空空如也");
            return false;
        }
        Integer index = utilView.getUserInputInteger("请输入专栏编号：");
        if (index > columnDTOList.size()  || index < 1) {
            utilView.showMessage("没有对应的栏目");
            return false;
        }
        try{
            ColumnDTO columnDTO = columnDTOList.get(index - 1);
            Integer columnId = columnDTO.getId();
            List<ArticleDTO> articleDTOList = articleService.listArticleByColumnId(columnId);
            columnView.showColumn(columnDTO, articleDTOList);
            MainController.currentColumnDTO = columnDTO;
            MainController.currentArticleDTOList = articleDTOList;
            return true;
        }catch (Exception e){
            e.printStackTrace();

        }
        return false;
    }

    public static void searchColumn() {
        try {
            String key = ColumnView.getInputString();
            if (key == null) {
                utilView.showMessage("输入不可为空！");
                return;
            }
            List<ColumnDTO> list = columnService.searchColumnsByName(key);
            if (list.isEmpty()) {
                utilView.showMessage("搜索结果为空");

            }
            for (int i = 0; i < list.size(); i++) {
                ColumnView.showColumn(i, list.get(i));
            }
            MainController.currentColumnDTOList = list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
